<?php
declare (strict_types=1);

namespace app\organization\controller\learn;

use app\common\controller\Frontend;
use app\common\controller\SysBackend;
use app\common\model\banxi\learn\LearnSubjectQuestionRecord as LearnSubjectQuestionRecordModel;
use app\common\model\banxi\organization\Organization;
use think\db\exception\DbException;

class LearnSubjectQuestionRecord extends Frontend
{
    protected array $noNeedLogin = [];
    protected array $noNeedPermission = [];

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new LearnSubjectQuestionRecordModel();
    }


    /**
     * 分页查询
     * @return void
     * @throws DbException
     */
    public function listPage(): void
    {
        $param = $this->request->param(['index', 'limit', 'orderBy', "query"]);
        //查询条件解析
        $where = get_query($param);
        //获取分页数据
        $pageParams = get_page_params($param);
        $list = $this->model->with([
            'student' => function ($query) {
                $query->field('id,name');
            },
            'trainer' => function ($query) {
                $query->field('id,name');
            },
            'subjectTeach' => function ($query) {
                $query->field('id,name');
            },
            'subjectTeachChapter' => function ($query) {
                $query->field('id,name');
            },
            'studentOrganInfo' => function ($query) {
                $query->field('id,name');
            }
        ])->where($where)
            ->order(['id' => 'asc'])
            ->paginate([
                'page' => $pageParams['page'],
                'list_rows' => $pageParams['limit'],
            ])->each(function ($item) {
                if ($item['studentOrgId'] == 0) {
                    $item['studentOrganInfo'] = (new Organization())->getOrganizationInfo();
                }
            });
        $this->ok('操作成功', result_row($list));
    }

    /**
     * 查询
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function get()
    {
        $param = $this->request->param(["query" => []]);
        if (empty($param['query'])) {
            $this->fail('请传递你要查询的记录');
        }
        $where = get_query($param);
        $info = $this->model->with([
            'student' => function ($query) {
                $query->field('id,name');
            },
            'trainer' => function ($query) {
                $query->field('id,name');
            },
            'subjectTeach' => function ($query) {
                $query->field('id,name');
            },
            'subjectTeachChapter' => function ($query) {
                $query->field('id,name');
            },
            'studentOrganInfo' => function ($query) {
                $query->field('id,name');
            }
        ])->where($where)->find();
        if (empty($info)) {
            $this->fail('记录不存在');
        }
        $this->ok('操作成功', $info);
    }

}
